<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Locale"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="com.ekingstar.framework.service.*"%>
<%@page import="com.ekingstar.framework.security.UserUtil"%>
<%@page import="java.util.List"%>
<%@page import="com.ekingstar.framework.domain.Function"%>
<%@page import="com.ekingstar.framework.domain.User"%>
<%
	User user = UserUtil.getCurrentUser();
	if (user == null){
		response.sendRedirect("/error.jsp");
	}
%>
<%@ include file="/common/include.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/css.css">
</head>
<body oncontextmenu="return false;" oncopy="return false;" onpaste="return false;" onselectstart="return false;" oncut="return false;">
<div>
	<div class="head">
		
		 <div class="top">
		  <div class="top_left"></div>
		  <div class="logo"></div>
		  <div class="top_right">
			<div class="control">
			</div>
		  </div>
		</div>
			
	
		<div class="menu">
			<ul>
				<%
				WebApplicationContext context =  WebApplicationContextUtils.getWebApplicationContext(application);	
				IFunctionService fs = (IFunctionService) context.getBean("functionService");
				IUserService us = (IUserService) context.getBean("userService");
				String id = request.getParameter("id");
			    String subId = request.getParameter("subId");
				Function parent = null;
				Function subFunction = null;
				  //String subId = request.getParameter("subId");
				%>
				<% if (id == null || id.trim().equals("")){
					id = "-1";
				   }
					if (id.trim().equals("-1")){
					parent = new Function("-1");
					parent.setLeaf(Boolean.TRUE);
					parent.setAddresss(basePath+"/welcome.jsp");
				%>
				<li class=onmleft></li><li class=onmbg>首&nbsp;&nbsp;页</li><li class=onmright></li>
				<% } else {%>
					<li><a href="?id=-1"> 首&nbsp;&nbsp;页</a></li>
				<%} %>
				<%
					
					
					List functions = fs.getFunctionsByUserId(UserUtil.getCurrentUserId());
					for (int i = 0 ;functions != null && i < functions.size();i++){
						Function fun = (Function)functions.get(i);
						if (Boolean.FALSE.equals(fun.getShow())){
							continue;
						}
						if (id == null || id.trim().equals("")){
							id = fun.getId();
						}
						if (fun.getId().equals(id)){
							parent = fun;
						}
						if (fun.getId().equals(id)){
							out.print("<li class=onmleft></li><li class=onmbg>"+fun.getName()+"</li><li class=onmright></li>");
						}
						else{
							out.print("<li><a href='?id="+fun.getId()+"'>"+fun.getName()+"</a></li>");
						}
					}
				%>
				
				<% 
					if (id.trim().equals("-2")){
						parent = new Function("-2");
						parent.setLeaf(Boolean.FALSE);
				%>
				<li class=onmleft></li><li class=onmbg>我的收藏</li><li class=onmright></li>
				<% } else {%>
					<li><a href="?id=-2">我的收藏</a></li>
				<%} %>
				<% 
					if (id.trim().equals("-3")){
						parent = new Function("-3");
						parent.setLeaf(Boolean.FALSE);
				%>
				<li class=onmleft></li><li class=onmbg>最近操作</li><li class=onmright></li>
				<% } else {%>
					<li><a href="?id=-3">最近操作</a></li>
				<%} %>
			</ul>
		</div>
		<div class="submenu">
			<ul>
				<%  
					
				    if (functions != null && id != null){
					Function fun = new Function(id);
					int i = functions.indexOf(fun);
					if (i >= 0){
						fun = (Function)functions.get(i);
						List subFunctions = fun.getChilden();
						
						for (int j = 0; subFunctions != null && j < subFunctions.size();j++){
							Function subFun = (Function)subFunctions.get(j);
							if (subId == null || subId.trim().equals("")) {
								subId = subFun.getId();
							}
							if (subFun.getId().equals(subId)){
								subFunction = subFun;
							}
							
					%>
					<li <%=subFun.getId().equals(subId)?"class='onsubm'":"" %>>
						<%if (subFun.getId().equals(subId)) {%>
							<%=subFun.getName()%>
						<%}else { %>
							<a href="?id=<%=id %>&subId=<%=subFun.getId() %>"><%=subFun.getName()%></a>
						<%} %>
						
					</li>  
					
					<% 			
							}
						}
					}
					%>
					<%
					//我的收藏
					if (id.equals("-2")){ 
					   Function addFav = new Function();
					   addFav.setId("-21");
					   addFav.setLeaf(Boolean.TRUE);
					   addFav.setName("收藏");
					   addFav.setAddresss(basePath+"/user/myFavarites.action");
					   List userFavarites =  us.getMyFavarites(UserUtil.getCurrentUserId());
					   if (userFavarites == null){
					   userFavarites = new java.util.ArrayList();
					   userFavarites.add(addFav);
					   }
					   else {
						   userFavarites.add(0,addFav);
					   }
					   for (int i = 0; i < userFavarites.size();i++){
							 Function fun = (Function)userFavarites.get(i);
							 if (subId == null || subId.trim().equals("")){
								 subId = fun.getId();
							 }
							 if (fun.getId().equals(subId)){
							  subFunction = fun;	 
							  out.println("<li class='onsubm'>"+fun.getName()+"</li>");
							 }
							 else {
								 out.println("<li><a  href='?id=-2&subId="+fun.getId()+"'>"+fun.getName()+"</a></li>");
							 }
						 }
					} 
					//最近操作
					else if (id.equals("-3")){
						 List userFuns = us.getLastFunctions(UserUtil.getCurrentUserId());
						 if (userFuns != null  && !userFuns.isEmpty()){
							 for (int i = 0; i < userFuns.size();i++){
								 Function fun = (Function)userFuns.get(i);
								 if (subId == null || subId.trim().equals("")){
									 subId = fun.getId();
								 }
								 if (fun.getId().equals(subId)){
								  subFunction = fun;	 
								  out.println("<li class='onsubm'>"+fun.getName()+"</li>");
								 }
								 else {
									 out.println("<li><a  href='?id=-3&subId="+fun.getId()+"'>"+fun.getName()+"</a></li>");
								 }
							 }
						 }
					} 
					%>
					
					
					
				
			</ul>
		</div>
	</div>
</div>
<% 
session.setAttribute("third_menus",null);
//session.removeAttribute("third_menus");

if (subFunction != null){
	session.setAttribute("third_menus",subFunction.getChilden());
	//out.println(session.getAttribute("third_menus"));
}
%>
<script>
	var menubar = top.frames["contents"].dtbar; 
	
	var main = top.frames["main"];
	<%
	if (parent != null && Boolean.TRUE.equals(parent.getLeaf())){
		out.println("main.location='"+parent.getAddresss()+"';");
	}
 	else if (subFunction != null && Boolean.TRUE.equals(subFunction.getLeaf())){
 		 out.println("main.location='"+subFunction.getAddresss()+"';");
		 us.updateLastFunction(UserUtil.getCurrentUserId(),subFunction);
	 }
	
	
	%>
	menubar.location = "menu.jsp";	
	
</script>
</body>

</html>
